﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class addEmployee : Form
    {
        editEmployeeRecordsModule parent;
        selection grandparent;
        TextBox salaryamnt, sssamnt, phamnt; //amount
        String salaryval, sssval, phval;
        public addEmployee(editEmployeeRecordsModule parent, selection grandparent)
        {
            this.parent = parent;
            this.grandparent = grandparent;
            InitializeComponent();
            salaryamnt = (TextBox)addEmployeeSalaryTextBox.Controls[1];
            sssamnt = (TextBox)addEmployeeSssTextBox.Controls[1];
            phamnt = (TextBox)addEmployeePhilhealthTextBox.Controls[1];
            salaryamnt.TextChanged += new EventHandler(amnt_TextChanged);
            sssamnt.TextChanged += new EventHandler(amnt_TextChanged);
            phamnt.TextChanged += new EventHandler(amnt_TextChanged);
            salaryamnt.Validating += new CancelEventHandler(amnt_Validating);
            sssamnt.Validating += new CancelEventHandler(amnt_Validating);
            phamnt.Validating +=new CancelEventHandler(amnt_Validating);
            salaryval = salaryamnt.Text;
            sssval = sssamnt.Text;
            phval = phamnt.Text;
        }

        public addEmployee()
        {
            InitializeComponent();
        }

        private void addEmployeeCancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void addEmployeeButton_Click(object sender, EventArgs e)
        {
            Boolean nameListContains = false;
            Employee employee = new Employee();
            string name = addEmployeeLastNameTextBox.Text+", "+addEmployeeFirstNameTextBox.Text;

            employee.firstName = addEmployeeFirstNameTextBox.Text;
            employee.lastName = addEmployeeLastNameTextBox.Text;
            employee.salary = (double)addEmployeeSalaryTextBox.Value;
            employee.payingBasis = addEmployeePayingBasisDropDownList.SelectedIndex;
            employee.position = addEmployeePositionDropDownList.SelectedIndex;
            employee.workHours = (int)addEmployeeWorkHoursTextBox.Value;
            employee.contractStart = DateTime.Now.ToString("yyyy-MM-dd");
            employee.contractEnd = addEmployeeContractEnd.Value.ToString("yyyy-MM-dd");
            employee.debt = 0;
            employee.SSS = (double)addEmployeeSssTextBox.Value;
            employee.PhilHealth = (double)addEmployeePhilhealthTextBox.Value;
            employee.employeeStatus = 0;
            employee.bonusesDeductions = 0;
            foreach (string N in parent.getEmployeesList().Items)
            {
                if (string.Compare(N, name, true) == 0)
                {
                    nameListContains = true;
                }
            }
            if (addEmployeeFirstNameTextBox.Text.Equals("") || addEmployeeLastNameTextBox.Text.Equals("") || 
                addEmployeePayingBasisDropDownList.SelectedIndex == -1 || addEmployeePositionDropDownList.SelectedIndex == -1)
            {
                MessageBox.Show("No field should be empty.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (addEmployeeWorkHoursTextBox.Value==0)
            {
                MessageBox.Show("Work hours cannot be set to 0.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (DateTime.Compare(addEmployeeContractEnd.Value,DateTime.Now)<=0)
            {
                MessageBox.Show("Please set contract end at a future date.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (nameListContains)
            {
                MessageBox.Show("An employee with the same name already exists.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                bool success = new EmployeeController().addEmployee(employee);
                if (success)
                {
                    parent.initializeContent();
                    grandparent.reInitializeContent();
                    new LateAbsenceController().updateAttendance(employee.firstName, employee.lastName);
                    parent.getEmployeesList().SelectedIndex = parent.getEmployeesList().Items.IndexOf(employee.lastName + ", " + employee.firstName);
                    this.Close();
                }
            }
        }

        private void amnt_TextChanged(object sender, EventArgs e)
        {
            if (salaryamnt.Focused || sssamnt.Focused || phamnt.Focused)
            {
                salaryval = salaryamnt.Text;
                sssval = sssamnt.Text;
                phval = phamnt.Text;
            }
        }

        private void amnt_Validating(object sender, CancelEventArgs e)
        {
            if (multipleDecPoints(salaryval)||multipleDecPoints(sssval)||multipleDecPoints(phval))
            {
                MessageBox.Show("Invalid input; too many decimal points.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (multipleDash(salaryval) || multipleDash(sssval) || multipleDash(phval))
            {
                MessageBox.Show("Invalid input.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
                if (salaryval.Equals(".") || sssval.Equals(".") || phval.Equals("."))
                {
                    MessageBox.Show("Invalid input.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    double salarycheck = Double.Parse(salaryval);
                    double ssscheck = Double.Parse(sssval);
                    double phcheck = Double.Parse(phval);
                    if (salarycheck < (double)addEmployeeSssTextBox.Minimum || salarycheck > (double)addEmployeeSssTextBox.Maximum
                        || ssscheck < (double)addEmployeeSssTextBox.Minimum || ssscheck > (double)addEmployeeSssTextBox.Maximum
                        || phcheck < (double)addEmployeeSssTextBox.Minimum || phcheck > (double)addEmployeeSssTextBox.Maximum)
                        MessageBox.Show("Invalid input; out of range.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
        }

        private bool multipleDash(String num)
        {
            int nDash = 0;
            int i = 0;
            while (nDash < 2 && i < num.Length)
            {
                if (num[i] == '-')
                    nDash++;
                i++;
            }
            if (nDash < 2)
                return false;
            else
                return true;
        }

        private bool multipleDecPoints(String num)
        {
            int nPoints = 0;
            int i = 0;
            while (nPoints < 2 && i < num.Length)
            {
                if (num[i] == '.')
                    nPoints++;
                i++;
            }
            if (nPoints < 2)
                return false;
            else
                return true;
        }
    }
}
